`
https://leetcode.cn/problems/xiao-zhang-shua-ti-ji-hua/
`

/**
 * @param {number[]} time
 * @param {number} m
 * @return {number}
 */
var minTime = function (time, m) {
  const n = time.length

  // 每天最多耗时 lim ，是否能完成计划
  const check = (lim) => {
    let useDay = 1, totalTime = 0, maxTime = 0
    for (let i = 0; i < n; i++) {
      const nextTime = Math.min(maxTime, time[i])
      if (nextTime + totalTime <= lim) {
        // 今天还能学
        totalTime += nextTime
        maxTime = Math.max(maxTime, time[i])
      } else {
        // 今天没时间学了
        useDay++
        totalTime = 0
        maxTime = time[i]
      }
    }
    return useDay <= m
  }

  let left = -1, right = time.reduce((acc, cur) => acc + cur)
  while (left + 1 < right) {
    const mid = left + Math.floor((right - left) / 2)
    if (check(mid)) {
      right = mid
    } else {
      left = mid
    }
  }
  return right
};